﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace Light_Notes
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class Options : Window
    {
        private Light_Notes.MainWindow mainWindow;
        private HotkeyEx hotkeyEx;
        private bool winKey;

        public Options()
        {
            InitializeComponent();

            mainWindow = App.Current.MainWindow as Light_Notes.MainWindow;
            autostart.IsChecked = Properties.Settings.Default.autostart;
            hotkeyEx = new HotkeyEx(Properties.Settings.Default.hotkey);
            winKey = false;
            RefreshText();
            folder.Text = Properties.Settings.Default.folder;
        }

        private void RefreshText()
        {
            string s;
            if (hotkeyEx.KeyCode == Keys.None)
            {
                if (hotkeyEx.Alt || hotkeyEx.Ctrl || hotkeyEx.Shift || hotkeyEx.WindowsKey)
                    s = "?";
                else
                    s = "None";
            }
            else
                s = hotkeyEx.KeyCode.ToString();
            if (hotkeyEx.Alt) s = "Alt" + " + " + s;
            if (hotkeyEx.Ctrl) s = "Ctrl" + " + " + s;
            if (hotkeyEx.Shift) s = "Shift" + " + " + s;
            if (hotkeyEx.WindowsKey) s = "Win" + " + " + s;
            hotkey.Text = s;
        }

        private void Save()
        {
            if (Properties.Settings.Default.autostart != autostart.IsChecked.Value)
            {
                Properties.Settings.Default.autostart = autostart.IsChecked.Value;
                mainWindow.RegisterAutostart();
            }

            if (Properties.Settings.Default.hotkey != hotkeyEx.ToInt())
            {
                Properties.Settings.Default.hotkey = hotkeyEx.ToInt();
                mainWindow.RegisterHotkey();
            }

            if (Properties.Settings.Default.folder != folder.Text && folder.Text.Length > 0)
            {
                Properties.Settings.Default.folder = folder.Text;
                mainWindow.Reload();
            }

            Properties.Settings.Default.Save();
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter: Save(); Close(); break;
                case Key.Escape: Close(); break;
            }
        }

        private void Move(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            Save();
            Close();
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void hotkey_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            hotkeyEx.Alt = ((Keyboard.Modifiers & ModifierKeys.Alt) != 0);
            hotkeyEx.Ctrl = ((Keyboard.Modifiers & ModifierKeys.Control) != 0);
            hotkeyEx.Shift = ((Keyboard.Modifiers & ModifierKeys.Shift) != 0);
            hotkeyEx.WindowsKey = winKey;
            if (e.Key == Key.LWin || e.Key == Key.RWin) winKey = true;
            if (e.Key == Key.LeftAlt || e.Key == Key.RightAlt ||
                e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl ||
                e.Key == Key.LeftShift || e.Key == Key.RightShift ||
                e.Key == Key.LWin || e.Key == Key.RWin)
                hotkeyEx.KeyCode = Keys.None;
            else
                hotkeyEx.KeyCode = (Keys)KeyInterop.VirtualKeyFromKey(e.Key);
            RefreshText();
            e.Handled = true;
        }

        private void hotkey_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.LWin || e.Key == Key.RWin) winKey = false;
            if (hotkeyEx.KeyCode == Keys.None)
            {
                hotkeyEx.Alt = false;
                hotkeyEx.Ctrl = false;
                hotkeyEx.Shift = false;
                hotkeyEx.WindowsKey = false;
                RefreshText();
            }
            e.Handled = true;
        }

        private void buttonBrowse_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.Description = "Specify the folder for storing notes. " +
                "If you are a Dropbox user, you can choose a folder under your Dropbox " +
                "so that notes will be synchronized automatically";
            dlg.SelectedPath = folder.Text;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                folder.Text = dlg.SelectedPath;
        }
    }
}
